require 'rexml/document'
require 'family_tree_api/person/assertions/name'
require 'family_tree_api/person/assertions/gender'
require 'family_tree_api/person/assertions/event'
require 'family_tree_api/person/assertions/fact'
require 'family_tree_api/person/assertions/relationship'
require 'family_tree_api/person/assertions/ordinance'


module FamilyTreeApi
  module Person
    class UpdateResponse
      attr_accessor :id, :names, :genders, :events, :facts, :relationships
      
      def parse(p_element)
        @id = p_element.attributes["id"]
        @names = REXML::XPath.match(p_element, "assertions/name").collect { |e| Assertion::Name.parse_response e }
        @genders = REXML::XPath.match(p_element, "assertions/gender").collect { |e| Assertion::Gender.parse_response e }
        @events = REXML::XPath.match(p_element, "assertions/event").collect { |e| Assertion::Event.parse_response e }
        @facts = REXML::XPath.match(p_element, "assertions/fact").collect { |e| Assertion::Fact.parse_response e }
        @relationships = REXML::XPath.match(p_element, "assertions/relationship").collect { |e| Assertion::Relationship.parse_response e }
      end
      
      def self.parse(xml_str)
        doc = REXML::Document.new(xml_str)
        r = UpdateResponse.new
        r.parse(doc.elements["familytree/persons[1]/person"])
        r
      end
    end
  end
end
